#!/bin/sh
# This regression test is a part of SIPp.
# A pcap play (here play_dtmf) in a call whose SDP gives its port
# without [media_port] is sent from the -mp port, not from port 0.
. "`dirname "$0"`/../functions"

# Sending pcap plays needs a raw socket: run as root in a user and
# network namespace of our own.
if test -z "$SIPP_NETNS"; then
    if unshare -rn true 2>/dev/null; then
        SIPP_NETNS=1 exec unshare -rn "$0"
    fi
    init
    skip "no user network namespaces"
fi
init
"`get_sipp`" -v | grep -q PCAP || skip "no PCAP"
ip link set lo up 2>/dev/null || skip "cannot bring up lo"

rm -f rtp.port
perl -e '
    use Socket;$|=1;(socket(S,PF_INET,SOCK_DGRAM,17)&&
    bind(S,pack_sockaddr_in(26750,inet_aton("127.0.0.1")))&&
    setsockopt(S,SOL_SOCKET,SO_RCVTIMEO,pack("L!L!",5,0))&&
    ($sa=recv(S,$o,2048,0)))||die($!);
    ($port)=unpack_sockaddr_in($sa);print "$port\n";' >rtp.port &
sippbg -sf uas.xml -i 127.0.0.1 -p 25730 -m 1
sippfg -sf uac.xml -i 127.0.0.1 -p 25731 -m 1 -mp 26740 \
    127.0.0.1:25730 -timeout 8 -timeout_error >/dev/null 2>&1
status=$?
wait
port=`cat rtp.port`
rm -f rtp.port

if test $status -ne 0; then
    fail "UAC failed with $status"
elif test "$port" != 26740; then
    fail "RTP sent from port ${port:-none}, not 26740"
else
    ok
fi
